package com.rtu.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import com.rtu.dao.ILzRainGatherDataDao;
import com.rtu.dao.ILzRainStationDao;
import com.rtu.dao.IRainGatherDataDao;
import com.rtu.dao.IStationDao;
import com.rtu.domain.Station;
import com.rtu.dto.QueryStationDto;
import com.rtu.dto.QueryStationRainReportDto;
import com.rtu.dto.RainWaterDto;
import com.rtu.dto.RainWaterReportDto;
import com.rtu.dto.SiphonDto;
import com.rtu.dto.StationResponse;
import com.rtu.dto.YearSiphonDto;
import com.rtu.service.ILzRainStationService;
import com.rtu.service.IStationService;

@Service
public class LzRainStationServiceImpl implements ILzRainStationService{
	
	@Resource
	private ILzRainStationDao stationDao;
	@Resource
	private ILzRainGatherDataDao rainGatherDataDao;

	@Override
	public Map query(QueryStationDto dto) {
        Map map = new HashedMap();
        List<StationResponse> list = new ArrayList<StationResponse>();
        Integer count = stationDao.count();
        if (count == 0) {
            map.put("totalPage",0);
            map.put("totalCount",0);
        } else {
            int totalPage = (count % dto.getPageRowCount() == 0) ? count / dto.getPageRowCount() : count / dto.getPageRowCount() + 1;
            dto.setStart((dto.getPage() - 1) * dto.getPageRowCount());
            list = stationDao.query(dto);
            for(StationResponse r : list) {
            	if(StringUtils.isNotBlank(r.getCalculationFormula())) {
                	Station station = new Station();
                	station.setCalculationFormula(r.getCalculationFormula());
                	station.setId(r.getId());
                	r.setCurrentWaterLevel(rainGatherDataDao.queryData(station));
            	}
            }
            map.put("totalPage",totalPage);
            map.put("totalCount",count);
        }
        map.put("pageRowCount", dto.getPageRowCount());
        map.put("page",dto.getPage());
        map.put("list",list);
        return map;
	}

	@Override
	public Map queryStationById(int id) {
		Map map = new HashedMap();
		Station station = stationDao.queryById(id);
		map.put("station", station);
		return map;
	}

	@Override
	public int updateStation(Station station) {
		return stationDao.updateStation(station);
	}

	@Override
	public int insertStation(Station station) {
		return stationDao.insertStation(station);
	}
	
	@Override
	public int delStationById(int id) {
		return stationDao.delRainStationById(id);
	}
	
	@Override 
	public Map queryRainlist(QueryStationRainReportDto dto) {
		Map map = new HashedMap();
        List<RainWaterDto> list = new ArrayList<RainWaterDto>();     
        Integer count = 0;
        count = stationDao.countRainlist(dto);
    
        if (count == null) {
            map.put("totalPage",0);
            map.put("totalCount",0);
        } else {
        	int totalPage = (count % dto.getRows() == 0) ? count / dto.getRows() : count / dto.getRows() + 1;
            dto.setStart(dto.getOffset()  * dto.getRows());
            dto.setPageRowCount(dto.getRows());
            list = stationDao.queryRainlist(dto);
            map.put("totalPage",totalPage);
            map.put("totalCount",count);  
//        	int totalPage = (count % dto.getPageRowCount() == 0) ? count / dto.getPageRowCount() : count / dto.getPageRowCount() + 1;
//            dto.setStart((dto.getPage() - 1) * dto.getPageRowCount());
//            list = stationDao.queryRainlist(dto);
//            map.put("totalPage",totalPage);
//            map.put("totalCount",count);    
        }
        
        map.put("pageRowCount", dto.getRows());
        map.put("page",dto.getOffset());
        map.put("list",list);

        return map;
	}
	
	@Override 
	public Map queryRainYearlist(QueryStationRainReportDto dto) {
		Map map = new HashedMap();
       // List<RainWaterDto> list = new ArrayList<RainWaterDto>();  
        List<YearSiphonDto> list = new ArrayList<YearSiphonDto>(); //
        Integer count = 10;
       // count = stationDao.countRainYearlist(dto);
    
       // int totalPage = (count % dto.getRows() == 0) ? count / dto.getRows() : count / dto.getRows() + 1;
       // dto.setStart(dto.getOffset()  * dto.getRows());
       // dto.setPageRowCount(dto.getRows());
        list = stationDao.queryRainYearlist(dto);
        map.put("totalPage",1);
        map.put("totalCount",count);     
        
        map.put("pageRowCount", dto.getRows());
        map.put("page",dto.getOffset());
        map.put("list",list);

        return map;
	}
	
	//1:当前 2:today 3:yesterday 4:beforeyesterday
	@Override
	public  Double queryRainData(QueryStationRainReportDto dto,int time) {
		double vasum=0.0;
		Date todayDate = new Date();
		Date yesterdayDate = new Date(todayDate.getTime()-86400000L);
		Date beforeyesterday = new Date(todayDate.getTime()-2*86400000L);
		String currentHourstr = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH");
        currentHourstr = currentHourstr +"%";
        String todaystr = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        todaystr = todaystr +"%";
        String yesterdaystr = DateFormatUtils.format(yesterdayDate, "yyyy-MM-dd");
        String beforeyesterdaystr = DateFormatUtils.format(beforeyesterday, "yyyy-MM-dd");
        
		switch(time) {
			case 1:
		        dto.setBeginTime(currentHourstr);     
		        vasum = stationDao.queryTodayRain(dto);
				break;
			case 2:
		        dto.setBeginTime(todaystr);
		        vasum = stationDao.queryTodayRain(dto);
		        break;
			case 3:        
		        dto.setBeginTime(yesterdaystr + " 00:00:00"); 
		        dto.setEndTime(yesterdaystr + " 23:59:59"); 
		        vasum = stationDao.queryBeforeRain(dto); 
				break;
			case 4:   
		        dto.setBeginTime(beforeyesterdaystr + " 00:00:00"); 
		        dto.setEndTime(beforeyesterdaystr + " 23:59:59"); 
		        vasum = stationDao.queryBeforeRain(dto);
		        break;
			default: break;
			}
			return vasum;    
		}
	
	@Override
	public List<RainWaterReportDto> queryReportData(QueryStationRainReportDto dto) {
     	List<RainWaterReportDto> list = stationDao.queryRainReport(dto);
        return list;
	}
	
}
